﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unmanned_MSUI.Areas.Admin.Filters;

namespace Unmanned_MSUI.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class StaffInfoController : Controller
    {
        #region 构造函数

        private IStaffInfoBLL _staffInfoBLL;
        private IDepartmentInfoBLL _departmentInfoBLL;
        public StaffInfoController(
            IStaffInfoBLL staffInfoBLL
            , IDepartmentInfoBLL departmentInfoBLL
            )
        {
            _staffInfoBLL = staffInfoBLL;
            _departmentInfoBLL = departmentInfoBLL;
        }
        #endregion

        #region 显示视图
        //员工列表显示
        public IActionResult ListView()
        {
            return View();
        }

        //员工添加
        public IActionResult CreateStaffInfoView()
        {
            return View();
        }

        //员工编辑
        public IActionResult UpdateStaffInfoView()
        {
            return View();
        }

        //员工修改基本资料
        public IActionResult StaffSettingView()
        {
            return View();
        }

        //员工修改密码
        public IActionResult UpdateStaffPwdView()
        {
            return View();
        }
        #endregion

        #region 员工列表获取的接口（GetStaffInfo）
        /// <summary>
        /// 员工列表获取的方法
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="limit">每页数量</param>
        /// <param name="account">员工账号</param>
        /// <param name="staffName">员工姓名</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStaffInfo(int page, int limit, string account, string staffName)
        {
            //总条数
            int count;
            //调用BLL的获取员工方法
            List<GetStaffInfoDTO> list = _staffInfoBLL.GetStaffInfo(page, limit, account, staffName, out count);

            //返回结果
            ReturnResult result = new ReturnResult()
            {
                Code = 0,//layui要求状态码为0
                Msg = "获取成功！",
                Data = list,
                IsSuccess = true,
                Count = count
            };
            //返回结果
            return Json(result);
        }
        #endregion

        #region 添加员工的接口（CreateStaffInfo）

        [HttpPost]
        public IActionResult CreateStaffInfo([FromForm] StaffInfo staff)
        {
            string msg;
            //调用添加方法
            bool isSuccess = _staffInfoBLL.CreateStaffInfo(staff, out msg);
            //返回结果封装
            ReturnResult result = new ReturnResult();
            result.Msg = msg;
            result.IsSuccess = isSuccess;

            //判断是否成功
            if (isSuccess)
            {
                result.Code = 200;
            }
            return Json(result);
        }

        #endregion

        #region 更新修改员工的接口（UpdateStaffInfo）

        [HttpPost]
        public IActionResult UpdateStaffInfo([FromForm] StaffInfo staffInfo)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用方法
            string msg;
            bool isOk = _staffInfoBLL.UpdateStaffInfo(staffInfo, out msg);
            result.Msg = msg;//把返回的描述
            //判断是否成功
            if (isOk)
            {
                result.Code = 200;
                result.IsSuccess = isOk;
            }
            return Json(result);//返回结果
        }

        #endregion

        #region 员工软删除的接口（DeleteStaffInfo）
        /// <summary>
        /// 员工软删除的接口
        /// </summary>
        /// <param name="id">前端传进来的Id</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteStaffInfo(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "Id不能为空！";
                return Json(result);
            }
            //调用删除方法
            bool isOk = _staffInfoBLL.DeleteStaffInfo(id);
            //判断删除情况
            if (isOk)
            {
                result.Msg = "删除员工成功！";
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }

        #endregion

        #region 批量员工软删除的接口（DeleteStaffInfos）
        /// <summary>
        /// 批量员工软删除的接口
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteStaffInfos(List<string> ids)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (ids == null || ids.Count == 0)
            {
                result.Msg = "你还没有选择要删除的员工！";
                return Json(result);
            }
            //批量删除的方法
            bool isOk = _staffInfoBLL.DeleteStaffInfo(ids);
            //判断是否成功
            if (isOk)
            {
                result.Msg = "删除成功！";
                result.Code = 200;
            }
            else
            {
                result.Msg = "删除失败！";
            }
            //返回结果
            return Json(result);
        }

        #endregion

        #region 根据员工的id获取员工的信息（GetStaffInfoById）
        /// <summary>
        /// 根据员工的id获取员工的信息
        /// </summary>
        /// <param name="id">员工的Id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStaffInfoById(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "员工id不能为空";
                return Json(result);
            }
            //调用方法获取员工的信息
            var staffInfo = _staffInfoBLL.GetStaffInfoById(id);
            //部门的下拉列表信息
            var selectOption = _departmentInfoBLL.GetSelectOptions();
            //给返回对象赋值
            result.Msg = "获取成功！";
            result.Code = 200;
            result.IsSuccess = true;
            result.Data = new
            {
                staffInfo,
                selectOption
            };
            //返回结果
            return Json(result);
        }
        #endregion

        #region 更新员工信息的接口：修改密码（UpdateStaffPwd）
        /// <summary>
        /// 更新员工信息
        /// </summary>
        /// <param name="id">员工id</param>
        /// <param name="oldPwd">旧的密码</param>
        /// <param name="newPwd">新的密码</param>
        /// <param name="againPwd">确认密码</param>
        /// <param name="msg">返回的消息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateStaffPwd(string id, string oldPwd, string newPwd, string againPwd)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            string msg;//声明返回的消息
            result.IsSuccess = _staffInfoBLL.UpdateStaffPwd(id, oldPwd, newPwd, againPwd, out msg);//调用更新密码方法
            result.Msg = msg;//赋值消息给返回对象
            //判断是否修改成功
            if (result.IsSuccess)
            {
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }

        #endregion
    }
}
